﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using System.IO;

public partial class Pages_MyAlbum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["loginClient"] == null)
            {
                Response.Redirect("../Pages/Login.aspx");
            }
            else
            {
                ImagesUI image=new ImagesUI();
                Customer cus = (Customer)Session["loginClient"];

                if (cus.C_Status)
                {
                    DataList1.DataSource = image.GetByCusID(cus.Cusid);
                    DataList1.DataBind();
                }
                else
                {
                    Response.Redirect("../Pages/AccountLock.aspx");
                }
            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int ca=Convert.ToInt32(btn.CommandArgument);

        Response.Redirect("../Pages/AddSize.aspx?id="+ca);

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int ca = Convert.ToInt32(btn.CommandArgument);
        ImagesUI img = new ImagesUI();
        string filename = img.Get(ca).Tables[0].Rows[0]["I_URL"].ToString();
        int result = img.Delete(ca);
        if (result > 0)
        {
            string pathServ = Server.MapPath("~/" + filename);
            if (File.Exists(pathServ))
            {
                File.Delete(pathServ);
            }
            Response.Write("<script>alert('Delete successful'); window.location='../Pages/MyAlbum.aspx';</script>");
        }
        else
        {
            Response.Write("<script>alert('Delete successful');window.location='../Pages/MyAlbum.aspx';</script>");          
        }
        
    }
}
